<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
class aaa implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $i;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($i)
    {
        //
        $this->i = $i;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $arr        = [];
        $result = $this->artsRequest('blockchain_get_block', [$this->i]);//获取最新区块的交易
        if (!empty($result['result']['user_transaction_ids'])) {
            foreach ($result['result']['user_transaction_ids'] as $result_trx_id) {
                $contract_result = $this->artsRequest('blockchain_get_pretty_contract_transaction', [$result_trx_id]);//通过交易哈希  查询合约交易
                if ($contract_result !== 2) {
                    if ($contract_result['result']['to_contract_ledger_entry']['to_account'] == 'CON73xk16ETk4YTBRztyvXgu3cTVYKd6eZc1') {//判断是不是SMC合约交易
                        $is_confirm = $this->artsRequest('blockchain_get_pretty_transaction', [$contract_result['result']['orig_trx_id']]);
                        if ($is_confirm['result']['is_confirmed'] == true) {//判断是否确认
                            $arr[] = [$contract_result['result']['orig_trx_id'], $contract_result['result']['reserved'][1]];
                        }
                    }
                }
            }
        }
        if (count($arr) > 0) {
            foreach ($arr as $value) {
                $array_address = explode('|', $value[1]);
                if (strlen($array_address[0]) > 60) {
                    if (strpos($array_address[0], 'ACTHD6QuGvc5LoTAAoCmiTwWfq9pj3JwRuN8') !== false){
                        DB::table('arts_transfers')->insert([
                            'tx_id' => $value[0],
                            'address' => $array_address[0],
                            'num' => $array_address[1]
                        ]);
                    };
                }
            }
        }
    }

    /*
     * arts  请求方法
     * */
    public function artsRequest($method, $params)
    {
        $client     = new Client();
        $url        = 'http://47.95.123.33:8299/rpc';
        $loginField = base64_encode("user:password");
        $params     = [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => '000000' . $loginField,
            ],
            'json' => [
                'id' => 1,
                'method' => $method,
                'params' => $params,
            ],
        ];
        try {
            $response = $client->request('POST', $url, $params);
        } catch (\Exception $exception) {
            return 2;
        }
        $content = $response->getBody()->getContents();
        return json_decode($content, true);
    }
}
